home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / faq / wdj0497.zip / HEAPOLE.ZIP / ALFLOAT.CPP next >
C/C++ Source or Header  |  1997-02-03  |  1KB  |  51 lines

  1. /* alfloat.cpp - examine effect of misalined doubles */
  2. #include <assert.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <windows.h>
  6.  
  7. #define  ARRAY_SIZE (1024*2)
  8.  
  9. void Benchmark(double* Array, const char* Name)
  10.     {
  11.     int     i, j;
  12.     DWORD   Start, Stop;
  13. printf("Benchmark starts.\n");
  14.     Start   = GetTickCount();
  15.     for(i = 0; i < 1000; ++i)
  16.         {
  17.         for(j = 0; j < ARRAY_SIZE; ++j)
  18.             Array[j] = i;
  19.         for(j = 0; j < ARRAY_SIZE; ++j)
  20.             Array[j] += 3.5;
  21.         }
  22.     Stop    = GetTickCount();
  23.     printf("Array=0x%08x\n", Array);
  24.     printf("Milliseconds for '%s' = %d\n", Name, Stop - Start);
  25.     }
  26.  
  27. void main(void)
  28.     {
  29.     int     Gap=1;
  30.     void*   Address;
  31.     double* Array=0;
  32.  
  33.     Address = malloc((sizeof(double)*ARRAY_SIZE)+4);
  34.     if((long)Address & 0x0007)
  35.         Array   = (double*)Address;
  36.     else
  37.         Array   = (double*)((long)Address + 4);
  38.  
  39.     /* do it twice to reduce possible caching effects */
  40.     Benchmark(Array, "4-byte aligned");
  41.     Benchmark(Array, "4-byte aligned");
  42.  
  43.     if(!((long)Address & 0x0007))
  44.         Array   = (double*)Address;
  45.     else
  46.         Array   = (double*)((long)Address+ 4);
  47.  
  48.     Benchmark(Array, "8-byte aligned");
  49.     Benchmark(Array, "8-byte aligned");
  50.     }
  51.